<?php

namespace app\erp\controller;

use app\erp\model\Offerdb;

class Offer extends Base
{
    protected $db;
    protected $cols = array(
        array(
            'field' => 'offer_id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'supplier_name',
            'width' => 300,
            'title' => '供应商'
        ),
        array(
            'field' => 'create_time',
            'width' => 160,
            'title' => '申请时间'
        ),
        array(
            'field' => 'baobei_order_no',
            'width' => 160,
            'title' => '报备单号'
        ),
        array(
            'field' => 'pingzheng_url',
            'width' => 160,
            'title' => '报价单凭证',
            'templet' => '#pingzheng_urlTpl'
        ),
        array(
            'field' => 'audit_state',
            'width' => 100,
            'title' => '审核状态',
            'templet' => '#audit_stateTpl'
        ),
        array(
            'field' => 'audit_admin_name',
            'width' => 120,
            'title' => '审核人'
        ),
        array(
            'field' => 'audit_time',
            'width' => 160,
            'title' => '审核时间'
        ),
        array(
            'fixed' => 'right',
            'width' => 80,
            'title' => '操作',
            'toolbar' => '#offer_tool'
        )
    );
    public $post = array();

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
        $this->db = new Offerdb ();
    }

    public function index()
    {
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    public function views($offer_id)
    {
        $this->db->param ['offer_id'] = $offer_id;
        $data = $this->db->ajaxFind();
        $this->assign('data', $data);
        $this->assign('offer_id', $offer_id);
        return $this->fetch();
    }

    public function ajaxList()
    {
        $this->db->param = $this->post;
        $return = $this->db->ajaxSelect();
        $this->layuiReturn($return ['data'], $return ['count']);
    }

    public function saveData()
    {
        $this->db->param = $this->post;
        $return = $this->db->ajaxSave();
        if ($return != 'success') {
            $this->layuiReturn($return, 0, $return, -1);
        } else {
            $this->layuiReturn('保存成功');
        }
    }

    /**
     *
     * @todo 删除
     */
    public function ajaxDelete()
    {
        $this->db->param = $this->post;
        $this->db->ajaxDelete();
        $this->layuiReturn('删除成功', 0);
    }
}